﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace A2DFramework.CookieService
{
    public static class CookieService
    {
        #region Check Parameters

        private static void CheckKey(string key)
        {
            if (key == null || key == string.Empty)
            {
                throw new ArgumentNullException("key", "key should never be null or empty.");
            }
        }

        private static void CheckValue(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "value should never be null or empty.");
            }
        }

        #endregion

        #region Add Cookie
        public static void Add(string key, string value)
        {
            CheckKey(key);
            CheckValue(value);

            HttpCookie responseCookie = new HttpCookie(key);
            responseCookie.Value = HttpContext.Current.Server.UrlEncode(value);
            //responseCookie.Secure = Config.SSLCookie;

            HttpContext.Current.Response.Cookies.Add(responseCookie);
        }
        public static void Add(string key, string value, DateTime expirationDate, string domainName = null)
        {
            CheckKey(key);
            CheckValue(value);

            HttpCookie responseCookie = new HttpCookie(key);
            responseCookie.Value = HttpContext.Current.Server.UrlEncode(value);
            //responseCookie.Secure = Config.SSLCookie;

            if (expirationDate > new DateTime(1900, 1, 1))
                responseCookie.Expires = expirationDate;

            if (!string.IsNullOrEmpty(domainName))
            {
                responseCookie.Domain = domainName;
            }

            HttpContext.Current.Response.Cookies.Add(responseCookie);
        }

        #endregion

        #region Get Cookie Value

        public static string Get(string key)
        {
            CheckKey(key);

            string value = string.Empty;

            HttpCookie requestCookie = HttpContext.Current.Request.Cookies[key];
            if (requestCookie != null)
            {
                value = HttpContext.Current.Server.UrlDecode(requestCookie.Value);
            }

            return value;
        }

        #endregion

        #region Remove Cookie

        public static void Remove(string key, string domainName = null)
        {
            Add(key, "", DateTime.Now.AddDays(-10), domainName);
        }

        public static void RemoveAll(string domainName)
        {
            foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
            {
                Add(key, "", DateTime.Now.AddDays(-10), domainName);
            }
        }

        #endregion

        public static void Resume(string key, int slidingMinutes)
        {
            CheckKey(key);

            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddMinutes(slidingMinutes);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
}
